﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iCPUApp.Code;
using System.Diagnostics;

namespace iCPUApp
{
    public partial class MainForm : Form
    {
        Processor cpu;
        Mainboard mainboard;
        List<string> cpus;
        BIOS bios;
        PerformanceCounter pcCore1;
        PerformanceCounter pcCore2;
        
        public MainForm()
        {
            InitializeComponent();
            cpus = Processor.GetAllCPUList();
            cpu = new Processor(cpus[0]);

            pcCore1 = new PerformanceCounter("Processor", "% Processor Time");
            pcCore1.InstanceName = "0";

            pcCore2 = new PerformanceCounter("Processor", "% Processor Time");
            pcCore2.InstanceName = "1";

            mainboard = new Mainboard();
            bios = new BIOS();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            bgWorker.RunWorkerAsync();
         }
    
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            /* Processor things */
            lblManufacturer.Text = cpu.GetManufacturer();
            lblName.Text = cpu.GetName();
            lblAddressWidth.Text = cpu.GetAddressWidth().ToString();
            lblDataWidth.Text = cpu.GetDataWidth();
            lblProcessorArchitecture.Text = cpu.GetArchitecture();
            lblAvailablity.Text = cpu.GetAvailability();
            lblCPUStatus.Text = cpu.GetCPUStatus();
            lblCurrentClockSpeed.Text = cpu.GetCurrentClockSpeed();
            lblCurrentVoltage.Text = cpu.GetCurrentVoltage();
            lblDeviceID.Text = cpu.GetDeviceID();
            lblExtClock.Text = cpu.GetExternalClockFreq();
            lblDesc.Text = cpu.GetDescription();
            lblFamiliy.Text = cpu.GetFamiliy();
            lblPowerManagement.Text = cpu.GetPowerManagementSupported();
            lblProcessorType.Text = cpu.GetProcessorType();
            lblVoltageCap.Text = cpu.GetVoltageCap();
            lblLCacheSize.Text = cpu.GetL2CacheSize();
            lblNumberofCores.Text = cpu.GetNumberofCores();
            lblNumberofLogicalCPU.Text = cpu.GetNumberofLogicalProcessor();
            lblLCacheSize.Text = cpu.GetL2CacheSize();
            lblL2CacheSpeed.Text = cpu.GetL2CacheSpeed();
            lblL3CacheSize.Text = cpu.GetL3CacheSize();
            lblL3CacheSpeed.Text = cpu.GetL3CacheSpeed();
            lblMaxClockSpeed.Text = cpu.GetMaxClockSpeed();


            /* Main board */
            lblMainboardSerial.Text = mainboard.SerialNumber;
            lblMainboardManufacturer.Text = mainboard.Manufacturer;
            lblMainboardVersion.Text = mainboard.Version;
            lblMainboardWidth.Text = mainboard.Width;
            lblMainboardHeight.Text = mainboard.Height;
            lblMainboardDepth.Text = mainboard.Depth;
            lblMainboardHostingboard.Text = mainboard.HostingBoard;
            lblMainboardHotswappable.Text = mainboard.HotSwappable;
            lblMainboardIsRemovable.Text = mainboard.IsRemovable;
            lblMainboardProduct.Text = mainboard.Product;
          
            /* BIOS */
            lblBiosVersion.Text = bios.Version;
            lblBiosIdentification.Text = bios.IdentificationCode;
            lblbiosManufacturer.Text = bios.Manufacturer;
            lblBiosCurrentLanguage.Text = bios.CurrentLanguage;
            lblBiosDescription.Text = bios.Description;
            lblSMBIOSVersion.Text = bios.SMBIOSBIOSVersion;
            lblSMBIOSMajorVersion.Text = bios.SMBIOSMajorVersion;
            lblSMBIOSMinorVersion.Text = bios.SMBIOSMinorVersion;
            lblPrimaryBIOS.Text = bios.IsPrimaryBIOS;
            /* Bioschar*/
            foreach(string s in bios.BiosCharacteristics)
            {
                lbBiosChar.Items.Add(s);
            }
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Show();
            Application.OpenForms["Splash"].Hide();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

       /* Processor usage timer */
        private void timer1_Tick(object sender, EventArgs e)
        {
          
            prgTotalProcessorUsage.Value = Convert.ToInt32(pcProcessorUsage.NextValue());
            lblProcessorUsage.Text = "Total Processor Usage : (" +prgTotalProcessorUsage.Value+") %";

            pbCore1Usage.Value = Convert.ToInt32(pcCore1.NextValue());
            lblCore1Usage.Text = "Core 1 Usage :("+pbCore1Usage.Value+") %";
            pbCore2Usage.Value = Convert.ToInt32(pcCore2.NextValue());
            lblCore2Usage.Text = "Core 2 Usage :(" + pbCore2Usage.Value + ") %";
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("iCPU v1.0\n\nCoded By Kirtan Patel(kirtan007@gmail.com)");
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                this.Height = 536;
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                this.Height = 328;
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                this.Height = 492;
            }
        }
    }
}
